﻿Imports Infragistics.WebUI.UltraWebNavigator

Public Class AwardAdmin
    Inherits System.Web.UI.Page
    Private et As New ETModelContainer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BuildTree()

        End If
    End Sub

    Protected Sub cmdAddNew_Click(sender As Object, e As EventArgs) Handles cmdAddNew.Click

        Dim req As AwardRequirement = New AwardRequirement
        req.Requirement = "New Requirement Tree"

        et.AddToAwardRequirements(req)
        et.SaveChanges()

    End Sub

    Protected Sub cboAwardTree_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboAwardTree.SelectedIndexChanged
        trvReq.Nodes.Clear()

        Dim lnode As Node = New Node()
        lnode.Text = cboAwardTree.SelectedItem.Text
        lnode.Tag = CInt(cboAwardTree.SelectedValue)
        trvReq.Nodes.Add(lnode)
        MakeLevel(CInt(cboAwardTree.SelectedValue), lnode)
        trvReq.Nodes(0).Selected = True
        trvReq.Nodes(0).Expand(False)


        txtDesc.Text = String.Empty
        txtReq.Text = String.Empty
        dblChildren.Value = Nothing

    End Sub

    Private Sub MakeLevel(id As Integer, node As Node)
        Dim ar As List(Of AwardRequirement) = (From a In et.AwardRequirements Where a.ParentID = id).ToList

        For Each ax As AwardRequirement In ar

            Dim lnode As Node = New Node()
            lnode.Text = ax.Requirement
            lnode.Tag = ax.RequirementID

           
            node.Nodes.Add(lnode)
            MakeLevel(ax.RequirementID, lnode)
        Next


    End Sub

    Protected Sub trvReq_NodeClicked(sender As Object, e As Infragistics.WebUI.UltraWebNavigator.WebTreeNodeEventArgs) Handles trvReq.NodeClicked
        Dim reqid As Integer = CInt(e.Node.Tag)

        Dim req = (From r In et.AwardRequirements Where r.RequirementID = reqid).SingleOrDefault


        txtReq.Text = req.Requirement
        txtDesc.Text = req.Description
        dblChildren.Value = 0
        If Not req.ChildrenRequired Is Nothing Then
            dblChildren.Value = CDbl(req.ChildrenRequired)
        End If



    End Sub

    Protected Sub cmdAddChild_Click(sender As Object, e As EventArgs) Handles cmdAddChild.Click

        Dim ar As AwardRequirement = New AwardRequirement
        ar.Requirement = "New Requirement"
        ar.ParentID = CInt(trvReq.SelectedNode.Tag)


        et.AddToAwardRequirements(ar)
        et.SaveChanges()

        Dim cnode As Node = New Node
        cnode.Text = "New Requirement"
        cnode.Tag = ar.RequirementID
        trvReq.SelectedNode.Nodes.Add(cnode)
    End Sub

    Private Sub BuildTree()
        Dim ar = From a In et.AwardRequirements Where a.ParentID Is Nothing
        cboAwardTree.Items.Clear()

        For Each aw As AwardRequirement In ar


            Dim orq = (From x In et.AwardRequirements Where x.ParentID Is Nothing)

            cboAwardTree.DataSource = orq

            cboAwardTree.DataTextField = "Requirement"
            cboAwardTree.DataValueField = "RequirementID"
            cboAwardTree.DataBind()

        Next
    End Sub

    Protected Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
        Dim reqid As Integer = CInt(trvReq.SelectedNode.Tag)


        Dim req = (From r In et.AwardRequirements Where r.RequirementID = reqid).SingleOrDefault


        req.Requirement = txtReq.Text
        req.Description = txtDesc.Text
        req.ChildrenRequired = dblChildren.ValueInt
        trvReq.SelectedNode.Text = txtReq.Text

        et.SaveChanges()

    End Sub
End Class